package com.goodving.guli.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.C3832t;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventGoalsUpdated;
import com.goodving.guli.bt.model.LoginData;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.profile.scale.C0638c;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.HwConnStatus;
import com.goodving.guli.bt.ui.PickAdapter;
import com.goodving.guli.model.DeviceSource;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.server.WebAPI;
import com.goodving.guli.server.WebResponse;
import com.goodving.guli.view.BitmapDescriptorFactory;
import com.goodving.guli.view.C3247b;
import com.goodving.guli.view.C4220a;
import com.loopj.android.http.AsyncHttpResponseHandler;

import cn.com.smartdevices.bracelet.config.C0886a;
import cz.msebera.android.httpclient.Header;
import de.greenrobot.event.EventBus;
import kankan.wheel.widget.WheelView;

/**
 * Created by john on 2015/12/25.
 */
public class HealthGoalsActivity extends SystemBarTintActivity implements View.OnClickListener {
    private static final String f8965a = "HealthGoalsActivity";
    private static final int f8966b = 1000;
    private PersonInfo f8967c;
    private WheelView f8968d;
    private TextView f8969e;
    private RelativeLayout f8970f;
    private Button f8971g;

    private void m13784a() {
        this.f8970f = (RelativeLayout) findViewById(R.id.other_tips_layout);
        this.f8968d = (WheelView) findViewById(R.id.person_info_goal_picker);
        this.f8968d.setVisibleItems(5).setCenterDrawable(R.drawable.wheel_custom_val_white_1).setCenterStyle(getString(R.string.step), R.color.main_ui_content_color, 12, BitmapDescriptorFactory.HUE_YELLOW, C4220a.bj).setViewAdapter(new PickAdapter(this, 2, 30, this.f8968d, getResources().getColor(R.color.main_ui_title_color), getResources().getColor(R.color.main_ui_content_color_light), getResources().getColor(R.color.main_ui_content_color_light_2), false, 50, 48, 45, 45, f8966b));
        this.f8969e = (TextView) findViewById(R.id.bracelet_title_info);
        if (this.f8967c.age < 17) {
            this.f8969e.setText(R.string.person_info_goal_info_young);
            if (this.f8967c.getDaySportGoalSteps() < 0) {
                this.f8967c.setDaySportGoals(C4220a.bT);
            }
        } else {
            this.f8969e.setText(R.string.person_info_goal_info);
            if (this.f8967c.getDaySportGoalSteps() < 0) {
                this.f8967c.setDaySportGoals(C4220a.bS);
            }
        }
        int daySportGoalSteps = (this.f8967c.getDaySportGoalSteps() - 2000) / f8966b;
        GuliLog.tracePrintInfo(f8965a, "curitem:" + daySportGoalSteps);
        this.f8968d.setCurrentItem(daySportGoalSteps);
        findViewById(R.id.cancel).setOnClickListener(this);
        this.f8971g = (Button) findViewById(R.id.confirm);
        this.f8971g.setOnClickListener(this);
        this.f8970f.setVisibility(View.INVISIBLE);
        this.f8971g.setVisibility(View.VISIBLE);
        GuliLog.tracePrintInfo(f8965a, "hasbindbracelet " + DeviceSource.hasBindBracelet());
//        GuliLog.tracePrintInfo(f8965a, "isMiliConnected " + C3844a.m25158c());
//        if (DeviceSource.hasBindBracelet() && !C3844a.m25158c()) {
//            this.f8970f.setVisibility(View.VISIBLE);
//            this.f8971g.setVisibility(View.INVISIBLE);
//        }
    }

    class ba extends C0638c {
        final /* synthetic */ HealthGoalsActivity f9692a;

        ba(HealthGoalsActivity healthGoalsActivity) {
            this.f9692a = healthGoalsActivity;
        }

        public void m7910a () { // m14499a
            super.m7910a();
        }

        public void m7912a (Object obj) { // m14500a
            super.m7912a(obj);
            if (!((Boolean) obj).booleanValue()) {
                C3247b.m22930a(this.f9692a, this.f9692a.getString(R.string.bt_operation_failed_tips), 0).show();
            }
        }

        public void m7914b (Object obj) { // m14501b
            super.m7914b(obj);
            C3247b.m22930a(this.f9692a, this.f9692a.getString(R.string.bt_operation_failed_tips), 0).show();
        }
    }

    private void m13785b() {
        int f = (this.f8968d.getRealCurrentItem() * f8966b) + C4220a.bZ;
        if (this.f8967c.getDaySportGoalSteps() == f) {
            setResult(-1);
            finish();
            return;
        }
        this.f8967c.setDaySportGoals(f);
        GuliLog.tracePrintInfo(f8965a, "goalStep = " + this.f8967c.getDaySportGoalSteps());
        if (DeviceSource.hasBindBracelet()) {
            new C3832t(this.f8967c.getDaySportGoalSteps(), new ba(this)).m9824f();
        }
        Keeper.keepPersonInfo(this.f8967c);
        m13786a(this.f8967c);
        GuliLog.tracePrintInfo(f8965a, "sync goal to mili ok.");
        EventBus.getDefault().post(new EventGoalsUpdated());
        Intent intent = new Intent();
        intent.putExtra(C4220a.ct, f);
        setResult(-1, intent);
        finish();
    }

    class bb extends AsyncHttpResponseHandler {
        final /* synthetic */ PersonInfo f9693a;
        final /* synthetic */ HealthGoalsActivity f9694b;

        bb(HealthGoalsActivity healthGoalsActivity, PersonInfo personInfo) {
            this.f9694b = healthGoalsActivity;
            this.f9693a = personInfo;
        }

        public void onFailure(int i, Header[] headerArr, byte[] bArr, Throwable th) {
            this.f9693a.setNeedSyncServer(2);
            Keeper.keepPersonInfo(this.f9693a);
            GuliLog.tracePrintInfo("HealthGoalsActivity", "fail, statusCode=" + i + ", content=" + Utils.m8035b(bArr));
        }

        public void onSuccess(int i, Header[] headerArr, byte[] bArr) {
            String b = Utils.m8035b(bArr);
            WebResponse a = WebResponse.getWebResponse(this.f9694b.getApplicationContext(), b);
            GuliLog.tracePrintInfo("HealthGoalsActivity", "statusCode=" + i + ", content=" + b);
            if (a.m11659c()) {
                this.f9694b.f8967c.clearNeedSyncServer();
            } else {
                this.f9693a.setNeedSyncServer(2);
            }
            Keeper.keepPersonInfo(this.f9693a);
        }
    }

    public void m13786a(PersonInfo personInfo) {
        LoginData f = C0886a.m9071f(getApplicationContext());
        personInfo.uid = f.uid;
        WebAPI.m11604a(f, personInfo, new bb(this, personInfo));
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.confirm:
                m13785b();
            case R.id.cancel:
                finish();
            default:
        }
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.health_goal_activity);
        this.f8967c = Keeper.readPersonInfo();
        m13784a();
    }

    protected void onDestroy() {
        super.onDestroy();
        C0668a.m8127a((Context) this, C0668a.dL, this.f8967c.getDaySportGoalSteps());
    }

    public void onEvent(HwConnStatus hwConnStatus) {
        if (hwConnStatus.m25408k()) {
            this.f8970f.setVisibility(View.INVISIBLE);
            this.f8971g.setVisibility(View.VISIBLE);
            return;
        }
        this.f8970f.setVisibility(View.VISIBLE);
        this.f8971g.setVisibility(View.INVISIBLE);
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != 16908332) {
            return super.onOptionsItemSelected(menuItem);
        }
        finish();
        return true;
    }

    protected void onPause() {
        super.onPause();
        C0668a.m8141b(C0668a.f3547Q);
        C0668a.m8138b((Activity) this);
    }

    protected void onResume() {
        super.onResume();
        C0668a.m8133a(C0668a.f3547Q);
        C0668a.m8124a((Activity) this);
    }
}

